unit uCorrecao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, StdCtrls, DBCtrls, EDBFind, DB, DBClient, Provider,
  SqlExpr, Grids, DBGrids, Buttons, ExtCtrls, Mask, ToolEdit,DBxPress;

type
  TfCorrecao = class(TForm)
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    sImovel: TSQLDataSet;
    dspImovel: TDataSetProvider;
    cdsImovel: TClientDataSet;
    dtsrcImovel: TDataSource;
    sImovelCOD_IMALU: TIntegerField;
    sImovelCOD_IMOBILIARIA: TStringField;
    sImovelDATA_LOCA: TDateField;
    sImovelDATA_VCONT: TDateField;
    sImovelPRAZO_REPASSE: TIntegerField;
    sImovelDIAPGTO: TStringField;
    cdsImovelCOD_IMALU: TIntegerField;
    cdsImovelCOD_IMOBILIARIA: TStringField;
    cdsImovelDATA_LOCA: TDateField;
    cdsImovelDATA_VCONT: TDateField;
    cdsImovelPRAZO_REPASSE: TIntegerField;
    cdsImovelDIAPGTO: TStringField;
    cLanPro: TClientDataSet;
    dLancPro: TDataSetProvider;
    sLanPro: TSQLDataSet;
    dtsrsLanpro: TDataSource;
    cLanProCOD_IMALU: TIntegerField;
    cLanProCOD_CONTAS: TIntegerField;
    cLanProVALOR: TFloatField;
    cLanProVENCIMENTO: TDateField;
    cLanProCOD_INQUILINO: TIntegerField;
    cLanProCOD_PROPRIETARIO: TIntegerField;
    cLanProATIVO: TStringField;
    sLanProCOD_IMALU: TIntegerField;
    sLanProCOD_CONTAS: TIntegerField;
    sLanProVALOR: TFloatField;
    sLanProVENCIMENTO: TDateField;
    sLanProCOD_INQUILINO: TIntegerField;
    sLanProCOD_PROPRIETARIO: TIntegerField;
    sLanProATIVO: TStringField;
    sLanInq: TSQLDataSet;
    IntegerField1: TIntegerField;
    IntegerField2: TIntegerField;
    FloatField1: TFloatField;
    DateField1: TDateField;
    IntegerField3: TIntegerField;
    IntegerField4: TIntegerField;
    StringField1: TStringField;
    dLanInq: TDataSetProvider;
    cLanInq: TClientDataSet;
    IntegerField5: TIntegerField;
    IntegerField6: TIntegerField;
    FloatField2: TFloatField;
    DateField2: TDateField;
    IntegerField7: TIntegerField;
    IntegerField8: TIntegerField;
    StringField2: TStringField;
    dtsrcLanInq: TDataSource;
    DBGrid3: TDBGrid;
    EvDBFind1: TEvDBFind;
    sImovelNOME_INQUILINO: TStringField;
    sImovelNOME_PROPRIETARIO: TStringField;
    cdsImovelNOME_INQUILINO: TStringField;
    cdsImovelNOME_PROPRIETARIO: TStringField;
    EvDBFind2: TEvDBFind;
    EvDBFind3: TEvDBFind;
    sLanProNOME_PROPRIETARIO: TStringField;
    cLanProNOME_PROPRIETARIO: TStringField;
    DBText2: TDBText;
    sLanInqNOME_INQUILINO: TStringField;
    cLanInqNOME_INQUILINO: TStringField;
    DBText1: TDBText;
    BitBtn1: TBitBtn;
    sImovelCOD_INQUILINO: TIntegerField;
    sImovelCOD_PROPRIETARIO: TIntegerField;
    cdsImovelCOD_INQUILINO: TIntegerField;
    cdsImovelCOD_PROPRIETARIO: TIntegerField;
    sLanProPARCELA: TIntegerField;
    cLanProPARCELA: TIntegerField;
    sLanInqPARCELA: TIntegerField;
    cLanInqPARCELA: TIntegerField;
    sLanInqCOD_LANCAMENTO: TIntegerField;
    cLanInqCOD_LANCAMENTO: TIntegerField;
    sLanProCOD_LANCAMENTO: TIntegerField;
    cLanProCOD_LANCAMENTO: TIntegerField;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    RadioGroup3: TRadioGroup;
    Label1: TLabel;
    BitBtn4: TBitBtn;
    DataInicial: TDateEdit;
    Edit1: TEdit;
    Label2: TLabel;
    procedure EvDBFind1KeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure RadioGroup2Click(Sender: TObject);
    procedure RadioGroup3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fCorrecao: TfCorrecao;
  TD: TTransactionDesc;

implementation

uses UDM, uImovel_aluguel;

{$R *.dfm}

procedure TfCorrecao.EvDBFind1KeyPress(Sender: TObject; var Key: Char);
var
 status : string;
begin
 if (key = #13) then
 begin
   if (RadioGroup1.ItemIndex = 0) then
     status := 'S';
   if (RadioGroup1.ItemIndex = 1) then
     status := 'N';
   if (cLanPro.Active) then
      cLanPro.Close;
   cLanPro.Params[0].AsInteger := cdsImovelCOD_IMALU.AsInteger;
   cLanPro.Params[1].AsString := status;
   cLanPro.Open;
   status := '';
   if (RadioGroup2.ItemIndex = 0) then
     status := 'S';
   if (RadioGroup2.ItemIndex = 1) then
     status := 'N';

   if (cLanInq.Active) then
      cLanInq.Close;
   cLanInq.Params[0].AsInteger := cdsImovelCOD_IMALU.AsInteger;
   cLanInq.Params[1].AsString := status;
   cLanInq.Open;
 end;
end;

procedure TfCorrecao.BitBtn1Click(Sender: TObject);
begin
  if (dm.cds_imo_aluguel.Active) then
      dm.cds_imo_aluguel.Close;
  dm.cds_imo_aluguel.Params[0].AsInteger := cdsImovelCOD_IMALU.AsInteger;
  dm.cds_imo_aluguel.Open;
  fImovel_aluguel:=TfImovel_aluguel.Create(Application);
  try
    fImovel_aluguel.ShowModal;
  finally
    fImovel_aluguel.Free;
  end;
end;

procedure TfCorrecao.FormShow(Sender: TObject);
begin
  cdsImovel.Open;
end;

procedure TfCorrecao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  cdsImovel.Close;
end;

procedure TfCorrecao.BitBtn2Click(Sender: TObject);
begin
  if (cLanPro.State in [dsEdit]) then
    cLanpro.ApplyUpdates(0);
end;

procedure TfCorrecao.BitBtn3Click(Sender: TObject);
begin
  if (cLanInq.State in [dsEdit]) then
    cLanInq.ApplyUpdates(0);
end;

procedure TfCorrecao.BitBtn4Click(Sender: TObject);
var
  ano, mes, dia, diatroca: word;
  Parcela : integer;
  texto, data, data1, TrocouDia : string;
  dataVenc, dataVenc1 : Tdate;
begin
  dataVenc := StrToDate(DataInicial.Text);
  if (RadioGroup3.ItemIndex = 1) then
  begin
     DecodeDate(dataVenc, ano,mes,dia);
     diatroca := Dia;
     Parcela := StrTOInt(Edit1.Text);
     cLanPro.First;
     while not cLanPro.Eof do
     begin
       dataVenc := EncodeDate(ano, mes, dia);
       if (TrocouDia = 'S') then
       begin
          dia := diatroca;
          TrocouDia := '';
       end;   
       TD.TransactionID := 1;
       TD.IsolationLevel := xilREADCOMMITTED;
       dm.scSge.StartTransaction(TD);
       texto := '';
       texto := 'update LANCAMENTOS SET VENCIMENTO = ';
       data := FormatDateTime('mm/dd/yyyy', dataVenc);
       texto := texto + QuotedStr(data);
       texto := texto + ',' + ' ATIVO = ' + QuotedStr('S');
       texto := texto + ',' + ' PARCELA = ' + IntToStr(Parcela);
       texto := texto + ' where COD_LANCAMENTO = ';
       texto := texto + IntToStr(cLanProCOD_LANCAMENTO.AsInteger);
       DM.scSge.ExecuteDirect(texto);
       dm.scSge.Commit(TD);
       cLanPro.Next;
       Parcela := Parcela + 1;
       mes := mes + 1;
       if (mes = 2 ) then
         if (dia = 29) then
         begin
           dia := 28;
           TrocouDia := 'S';
         end;
       if (mes > 12) then
       begin
         ano := ano + 1;
         mes := 1;
       end;
     end;
     EvDBFind1.SetFocus;
  end;

  if (RadioGroup3.ItemIndex = 0) then
  begin
     DecodeDate(dataVenc, ano,mes,dia);
     diatroca := Dia;
     Parcela := StrTOInt(Edit1.Text);
     cLanInq.First;
     while not cLanInq.Eof do
     begin
       dataVenc := EncodeDate(ano, mes, dia);
       if (TrocouDia = 'S') then
       begin
          dia := diatroca;
          TrocouDia := '';
       end;
       TD.TransactionID := 1;
       TD.IsolationLevel := xilREADCOMMITTED;
       dm.scSge.StartTransaction(TD);
       texto := '';
       texto := 'update LANCAMENTOS SET VENCIMENTO = ';
       data := FormatDateTime('mm/dd/yyyy', dataVenc);
       texto := texto + QuotedStr(data);
       texto := texto + ',' + ' ATIVO = ' + QuotedStr('S');
       texto := texto + ',' + ' PARCELA = ' + IntToStr(Parcela);
       texto := texto + ' where COD_LANCAMENTO = ';
       texto := texto + IntToStr(cLanInqCOD_LANCAMENTO.AsInteger);
       DM.scSge.ExecuteDirect(texto);
       dm.scSge.Commit(TD);
       cLanInq.Next;
       Parcela := Parcela + 1;
       mes := mes + 1;
       if (mes = 2 ) then
         if (dia = 29) then
         begin
           dia := 28;
           TrocouDia := 'S';
         end;
       if (mes > 12) then
       begin
         ano := ano + 1;
         mes := 1;
       end;
     end;
     EvDBFind1.SetFocus;
  end;
end;

procedure TfCorrecao.RadioGroup1Click(Sender: TObject);
begin
EvDBFind1.SetFocus;
end;

procedure TfCorrecao.RadioGroup2Click(Sender: TObject);
begin
EvDBFind1.SetFocus;
end;

procedure TfCorrecao.RadioGroup3Click(Sender: TObject);
begin
EvDBFind1.SetFocus;
end;

end.
